package shuzi.activity;

import java.util.List;
import shuzi.data.Database;
import shuzi.model.Brand;
import shuzi.util.Dialog;
import shuzi.util.Ifconnect;
import shuzi.util.MySharedPreferences;
import shuzi.util.Webserice;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.shuzi.offer.R;

public class FirstPageActivity extends Activity {

	/** 更新界面的handler   */
	private Handler handler = null;
	/** 开始的时间   */
	private Long firstTime;
	/** 结束请求的时间   */
	private Long endTime;
	/** 间隔时间   */
	private long spaceTime = 1000*3;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.first);
		initHandler();
		initData();
	}

	/**
	 * 初始化数据
	 */
	private void initData() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				Looper.prepare();
		       // WebImageUtil.shareWebImageUtil().getImageHelper().setLoadingResource(R.drawable.phone_defult);
				firstTime = System.currentTimeMillis();
				String data = Webserice.saveTermInfo("",
						MySharedPreferences.readStringSharedPreferences(
								FirstPageActivity.this,
								Database.LOGIN_FILE_NAME,
								Database.PHONE_NUMBER), MySharedPreferences
								.readStringSharedPreferences(
										FirstPageActivity.this,
										Database.LOGIN_FILE_NAME,
										Database.DELIVER_NUMBER),
						MySharedPreferences.readStringSharedPreferences(
								FirstPageActivity.this,
								Database.LOGIN_FILE_NAME,
								Database.DELIVER_ADDRESS));

				if (Webserice.isRegisterSuccess(data)) {
					
					handler.sendEmptyMessage(10);
				} else {
					if(data==null){
						handler.sendEmptyMessage(Database.HANDLER_REFRESH_ERROR);
						return;
					}
					if(!Ifconnect.checkConnect(FirstPageActivity.this)){
						Dialog.noNetDialog(FirstPageActivity.this);
					}else{		
						endTime = System.currentTimeMillis();
						while(endTime-firstTime<=1500){
							endTime = System.currentTimeMillis();
						}
							Intent in = new Intent();
							in.setClass(FirstPageActivity.this, RegistetActivity.class);
							
							startActivity(in);
							finish();
					
						
					//	overridePendingTransition(android.R.anim.fade_out, android.R.anim.fade_in);
						
						
					/*Dialog.register_dialog(FirstPageActivity.this,
							WifiControl.getLocalMacAddress(FirstPageActivity.this),
							handler);*/
					}
				}
				
				
				Looper.loop();
			}
		}).start();

		
	}

	/**
	 * 初始化Handler 
	 */
	private void initHandler() {
		handler  = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
				case Database.HANDLER_REFRESH_GRIDVIEW:
					Database.brandList = (List<Brand>) msg.obj;
					Intent intent = new Intent();
					intent.setClass(FirstPageActivity.this, MainActivity.class);
					endTime = System.currentTimeMillis();
					while(endTime-firstTime<=spaceTime){
						endTime = System.currentTimeMillis();
					}
					startActivity(intent);
					finish();
					break;
				case Database.HANDLER_REFRESH_ERROR:
					
					Dialog.netError(FirstPageActivity.this);
				
					break;
				case 10:
					Webserice.getMovieDataList(handler,
							Database.HANDLER_REFRESH_GRIDVIEW);

				default:
					break;
				}
			}
		};
	}
}
